package newcrunch.codeanalysis;

import org.w3c.dom.Element;
import org.w3c.dom.NodeList;

/**
 *
 * @author ccadm
 */
public class PluginUtil 
{
    private static int tabSize = 8;
    
    public static void setTabSize(int size)
    {
        tabSize = size;
    }
    
    /**
     * As the semantic analysis plugins treat tabs as occupying 
     * several columns, we need to convert the several columns
     * into one column for each tab that appears.
     * @param input
     * @param pos
     * @return 
     */
    public static int dispIndexToStrIndex(String input, int pos)
    {
        int index = 0;
        int tracker = 0;
        while (index < pos && tracker < input.length())
        {
            if (input.charAt(tracker) == '\t')
                index += tabSize;
            else
                index++;
            
            tracker++;
        }
        return tracker;
    }
    
    public static Element getFirstElement(Element root)
    {
        NodeList list = root.getChildNodes();
        
        for (int i = 0; i < list.getLength(); i++)
            if (list.item(i) instanceof Element)
                return (Element)list.item(i);
        
        return null;
    }    
}
